package proj1.ui;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;

import proj1.dao.Model;
import proj1.dao.Radiator;
import proj1.dao.Radiator.SIZE;
import proj1.dao.Vendor;
import proj1.util.Logger;

public class RadiatorDescription extends JPanel{
	private static Logger log = Logger.getLogger(RadiatorDescription.class);
	private static final long serialVersionUID = 1L;

	JTable radiatorTable;
	
	public RadiatorDescription(final ApplicationFrame mainFrame, final Model model){
		JPanel panel = new JPanel();
		panel.setLayout(new BoxLayout(panel, BoxLayout.PAGE_AXIS));
		String[][] data = {{"Part Number", ""},
				{"Core", ""},
				{"INHEAD", ""},
				{"OUTHEAD", ""},
				{"INCON", ""},
				{"OUTCON", ""},
				{"TMOUNT", ""},
				{"OILCOOL", ""},
				{"PRICE"," "},
				{"AMOUNT", "" }
			};
		String[] headers = {"Property", "Value"};
		radiatorTable = new JTable(data, headers){
	        private static final long serialVersionUID = 1L;

	        public boolean isCellEditable(int row, int column) {                
	                return column == 1;               
	        };
	    };
	    radiatorTable.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
		JScrollPane jsp = new JScrollPane(radiatorTable);
		Dimension d = new Dimension((int) jsp.getPreferredSize().getWidth(), 
									(radiatorTable.getRowHeight()+1)*11);
		
		
		final JComboBox<SIZE> sizeBox = new JComboBox<>();
		for(SIZE size : Radiator.SIZE.values())
			sizeBox.addItem(size);
		
		final JComboBox<Vendor> vendorBox = new JComboBox<>();
		for(Vendor vendor : Vendor.getVendors())
			vendorBox.addItem(vendor);
		
		JButton addRadiatorButton = new JButton("Add Radiator");
		addRadiatorButton.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent e){
				Vendor vendor = (Vendor) vendorBox.getSelectedItem();
				if (vendor == null){
					mainFrame.setErrorMessage("Select a Vendor");
					return;
				}
				
				SIZE size = (SIZE) sizeBox.getSelectedItem();
				if(size == null){
					mainFrame.setErrorMessage("Select a Size");
					return;
				}
				
				String partNum = (String) radiatorTable.getValueAt(0, 1);
				String core = (String) radiatorTable.getValueAt(1,1);
				String inhead = (String) radiatorTable.getValueAt(2, 1);
				String outhead = (String) radiatorTable.getValueAt(3, 1);
				String incon = (String) radiatorTable.getValueAt(4, 1);
				String outcon = (String) radiatorTable.getValueAt(5, 1);
				String tmount = (String) radiatorTable.getValueAt(6, 1);
				String oilcool  = (String) radiatorTable.getValueAt(7, 1);
				Double price = null; 
				Long amount = null; 
			
				if(partNum.isEmpty()){
					mainFrame.setErrorMessage("Part number required");
					return;
				}
				
				try {
					price = Double.parseDouble((String) radiatorTable.getValueAt(8, 1));
				} catch (NumberFormatException ex){
					log.error("Price is invalid. Should be in format '10.10'. Found " + price);
					mainFrame.setErrorMessage("Please enter a valid price.");
					return;
				}
				
				try {
					amount = Long.parseLong((String) radiatorTable.getValueAt(9, 1));
				} catch (NumberFormatException ex){
					log.error("Amount is invalid. Expected a number. Found " + amount);
					mainFrame.setErrorMessage("Please enter a valid amount");
					return;
				}
				
				Radiator r = new Radiator(partNum, core, inhead, outhead, incon, outcon, tmount, oilcool, price, amount);
				boolean success = r.save(model, size, vendor);
				
				if(success){
					mainFrame.replaceFrame(new RadiatorDescription(mainFrame, r));
				} else {
					mainFrame.setErrorMessage("Failed to save radiator.");
				}
			}
		});
		
		jsp.setPreferredSize(d);
		panel.add(jsp);
		panel.add(sizeBox);
		panel.add(vendorBox);
		panel.add(addRadiatorButton);
		add(panel, BorderLayout.CENTER);
		setVisible(true);
		
	}
	
	public RadiatorDescription(final ApplicationFrame mainFrame, Radiator r){
		JPanel panel = new JPanel();
		panel.setLayout(new BoxLayout(panel, BoxLayout.PAGE_AXIS));
		String[][] data = {{"Part Number", r.getPartNumber()},
				{"Core", r.getCore()},
				{"INHEAD",r.getInHead()},
				{"OUTHEAD",r.getOutHead()},
				{"INCON",r.getInCon()},
				{"OUTCON",r.getOutCon()},
				{"TMOUNT",r.getTMount()},
				{"OILCOOL",r.getOilCool()},
				{"PRICE","$"+r.getPrice().toString()},
				{"AMOUNT",r.getQuantity().toString()}};
		String[] headers = {"Property", "Value"};
		radiatorTable = new JTable(data, headers){
	        private static final long serialVersionUID = 1L;
	        public boolean isCellEditable(int row, int column) {                
	                return false;               
	        };
	    };
	    
		radiatorTable.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
		JScrollPane jsp = new JScrollPane(radiatorTable);
		Dimension d = new Dimension((int) jsp.getPreferredSize().getWidth(), 
									(radiatorTable.getRowHeight()+1)*11);
		jsp.setPreferredSize(d);
		panel.add(jsp);
		add(panel, BorderLayout.CENTER);
		setVisible(true);
	}
	@Override
	public String toString(){
		return "Radiator Description";
	}
}	
